#include "evos_port_mcu.h"

#include "my_led.h"
#include "mcu_config_adc.h"

typedef struct eos_sm_led_tag {
    eos_sm_t super;

    eos_u8_t status;
} eos_hsm_app_t;

eos_hsm_app_t st_hsm_app;

/* static state function ---------------------------------------------------- */
/**
 * a1 是状态a的子状态 当处在a1状态时 如果触发的事件a1状态处理不了
 * 则会交给a1上层状态a状态处理 详情请看HSM的定义
 * 状态图如下：
 *  -------a------             -------b------
 *  |      a1 -- |-----------> |             |
 *  --------------             --------------
 * HSM例子根据传感器的状态来切换状态
 * 默认状态为a a为a1上层状态
 * 当传感器状态为a时 探测到物体 adc_value < 1000 状态机切换到a1
 * 当传感器状态为a1时 物体消失 状态机切换到a
 * 当处在A大状态时，光源变强 切换至状态b
 */
static eos_ret_t app_state_init(eos_hsm_app_t * const me, eos_event_t const * const e);
static eos_ret_t app_state_a(eos_hsm_app_t * const me, eos_event_t const * const e);
static eos_ret_t app_state_a_1(eos_hsm_app_t * const me, eos_event_t const * const e);
static eos_ret_t app_state_b(eos_hsm_app_t * const me, eos_event_t const * const e);

/* api ---------------------------------------------------- */
void eos_app_hsm_init(void)
{
    eos_sm_init(&st_hsm_app.super, 1, EOS_NULL);
    eos_sm_start(&st_hsm_app.super, EOS_STATE_CAST(app_state_init));

    st_hsm_app.status = 0;
}

/* static state function ---------------------------------------------------- */
static eos_ret_t app_state_init(eos_hsm_app_t * const me, eos_event_t const * const e)
{
#if (EOS_USE_PUB_SUB != 0)
    EOS_EVENT_SUB(Event_Object_Appear);
    EOS_EVENT_SUB(Event_Object_Disappear);
    EOS_EVENT_SUB(Event_Light_Strength);
    EOS_EVENT_SUB(Event_Light_Reduce);
#endif
    // eos_event_pub_period(Event_Time_500ms, 500);

    // 初始化状态机 进入状态a
    return EOS_TRAN(app_state_a);
}

static eos_ret_t app_state_a(eos_hsm_app_t * const me, eos_event_t const * const e)
{
    switch (e->topic) {
        case Event_Init:
            return EOS_TRAN(app_state_a_1);
        case Event_Enter:
            me->status = 1;
            LED_ON;
            return EOS_Ret_Handled;
        case Event_Exit:
            return EOS_Ret_Handled;
        case Event_Object_Appear:
            //检测到物体
            return EOS_TRAN(app_state_b);
        case Event_Object_Disappear:
            //物体消失
            me->status=0;
            return EOS_Ret_Handled;
        case Event_Light_Strength:
            //光线增强
            me->status=3;
            return EOS_Ret_Handled;
        default:
            return EOS_SUPER(eos_state_top);
    }
}
static eos_ret_t app_state_a_1(eos_hsm_app_t * const me, eos_event_t const * const e)
{
    switch (e->topic) {
        case Event_Enter:
            me->status = 11;
            LED_OFF;
            return EOS_Ret_Handled;
        case Event_Exit:
            return EOS_Ret_Handled;
        case Event_Object_Disappear:
            //物体消失
            me->status=12;
            return EOS_Ret_Handled;
       
        default:
            return EOS_SUPER(app_state_a);
    }
}
static eos_ret_t app_state_b(eos_hsm_app_t * const me, eos_event_t const * const e)
{
    switch (e->topic) {
        case Event_Enter:
            me->status = 2;
            return EOS_Ret_Handled;
        case Event_Exit:
            return EOS_Ret_Handled;
        case Event_Light_Reduce:
            //光线减弱
            me->status=21;
            return EOS_Ret_Handled;
        case Event_Object_Disappear:
            //物体消失
            me->status=22;
            return EOS_Ret_Handled;
        default:
            return EOS_SUPER(app_state_a);
    }
}

void eos_app_test_event(void)
{
    extern ADC_Value st_adc_value;
    
    uint16_t u2_light_adc_value = st_adc_value.adc_1;
    uint16_t u2_object_adc_value = st_adc_value.adc_2;
    if(u2_object_adc_value<500){
        eos_event_pub_topic(Event_Object_Appear);
    }else if(u2_object_adc_value>3500){
        eos_event_pub_topic(Event_Object_Disappear); 
    }
    if(u2_light_adc_value<500){
        eos_event_pub_topic(Event_Light_Strength);
    }else if(u2_light_adc_value>2000){
        eos_event_pub_topic(Event_Light_Reduce); 
    }
}